package com.example.wannerone.myapplication123;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.os.Handler;
import android.os.Message;
import android.widget.Button;
import android.view.View;
import android.content.Intent;
import android.content.DialogInterface;
import android.widget.EditText;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.wannerone.http.HttpPostUtils;

import java.util.HashMap;
import java.util.Map;

public class changekeyActivity extends Activity {
    private String result;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_changekey);
        Button btn = (Button) findViewById(R.id.button9);
        final EditText oldPassword=(EditText) findViewById(R.id.editText2);
        final EditText newPassword=(EditText) findViewById(R.id.editText5);
        final EditText newAgain=(EditText) findViewById(R.id.editText6);
        btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                //判断两次新密码是否相同
                if(newPassword.getText().toString().equals(newAgain.getText().toString())){
                    //两次新密码相同
                    MyApplication application = (MyApplication) getApplication();
                    final Map<String, Object> userInfo = new HashMap<String, Object>();
                    userInfo.put("u_id",application.getU_id());
                    userInfo.put("oldPassword",oldPassword.getText().toString());
                    userInfo.put("newPassword", newPassword.getText().toString());
                    String jsonString = JSONObject.toJSONString(userInfo);
                    final String info = "info=" + jsonString;
                    new Thread() {
                        public void run() {
                            String response = HttpPostUtils.doPostRequest("/doorKey/modifyKey", info);
                            Message message = Message.obtain();
                            message.obj = response;
                            mHanlder.sendMessage(message);
                        }
                    }.start();
                }else{
                    //两次密码不同
                    new AlertDialog.Builder(changekeyActivity.this)
                            .setTitle("系统提示")
                            .setMessage("两次新密码不同，请重新输入")
                            .show();
                }
            }
        });
    }

    private Handler mHanlder = new Handler() {
        @Override
        public void handleMessage(Message message) {
            result = (String) message.obj;
            JSONObject resultJson = JSON.parseObject(result);
            String finalResult = resultJson.getString("result");
            System.out.println("结果是：" + finalResult);
            if (finalResult.equals("ok")) {
                //String u_id = resultJson.getString("u_id");
                //MyApplication application = (MyApplication) getApplication();
                //application.setU_id(Integer.parseInt(u_id));
                //System.out.println("application.getU_id=" + application.getU_id());
                System.out.println("开门密码修改成功");

                new AlertDialog.Builder(changekeyActivity.this)
                        .setTitle("系统提示")
                        .setMessage("密码修改成功！")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Intent intent = new Intent(changekeyActivity.this,safeysActivity.class);
                                //启动意图
                                startActivity(intent);
                                changekeyActivity.this.finish();
                            }   //确定按钮的点击事件
                        })
                        .show();
            } else
                System.out.println("用户名或密码错误");
        }
    };
}
